#include "Util.h"


Util::Util(void)
{
}


Util::~Util(void)
{
}

//	adjust letters to uppercase
std::string Util::toUpperCase(std::string str)
{
	std::string out = "";
	for (std::string::size_type i = 0; i < str.length(); i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
			out.push_back(str[i] + 'A' - 'a');
		else 
			out.push_back(str[i]);
	}
	return out;
}

bool Util::isNumber(std::string str)
{
	if (str.length() <= 0)
		return false;

	if (str[0] != '+' && str[0] != '-' && (str[0] < '0' || str[0] > '9'))
		return false;

	for (std::string::size_type i = 1; i < str.length(); i++)
	{
		if (str[i] < '0' || str[i] > '9')
			return false;
	}

	return true;
}

bool Util::isLiteral(std::string str)
{
	if (str.length() <= 0)
		return false;

	if ((str[0] < 'a' || str[0] > 'z') && (str[0] < 'A' || str[0] > 'Z'))
		return false;

	for (std::string::size_type i = 1; i < str.length(); i++)
	{
		if ((str[i] < 'a' || str[i] > 'z') && (str[i] < 'A' || str[i] > 'Z') && (str[i] < '0' || str[i] > '9'))
			return false;
	}

	return true;
}

bool Util::isEqual(std::string str1, std::string str2)
{
	if (str1.length() != str2.length())
		return false;

	for (std::string::size_type i = 0; i < str1.length(); i++)
	{
		char ch1 = str1[i];
		char ch2 = str2[i];
		if (ch1 >= 'A' && ch1 <= 'Z')
			ch1 += 'a' - 'A';
		if (ch2 >= 'A' && ch2 <= 'Z')
			ch2 += 'a' - 'A';

		if (ch1 != ch2)
			return false;
	}

	return true;
}

//	Check if a declared function name is legal
bool Util::isFuncNameLegal(std::string funcName)
{
	if (!isLiteral(funcName))
		return false;
	else if (isEqual(funcName, "CAR"))
		return false;
	else if (isEqual(funcName, "CDR"))
		return false;
	else if (isEqual(funcName, "CONS"))
		return false;
	else if (isEqual(funcName, "ATOM"))
		return false;
	else if (isEqual(funcName, "EQ"))
		return false;
	else if (isEqual(funcName, "NULL"))
		return false;
	else if (isEqual(funcName, "INT"))
		return false;
	else if (isEqual(funcName, "PLUS"))
		return false;
	else if (isEqual(funcName, "MINUS"))
		return false;
	else if (isEqual(funcName, "TIMES"))
		return false;
	else if (isEqual(funcName, "QUOTIENT"))
		return false;
	else if (isEqual(funcName, "REMAINDER"))
		return false;
	else if (isEqual(funcName, "LESS"))
		return false;
	else if (isEqual(funcName, "GREATER"))
		return false;
	else if (isEqual(funcName, "COND"))
		return false;
	else if (isEqual(funcName, "QUOTE"))
		return false;
	else if (isEqual(funcName, "DEFUN"))
		return false;

	return true;
}

//	Check if a declared varable name is legal
bool Util::isVarNameLegal(std::string varName)
{
	if (!isLiteral(varName))
		return false;
	else if (isEqual(varName, "NIL"))
		return false;
	else if (isEqual(varName, "T"))
		return false;

	return true;
}

//	Check if a literal excluded
bool Util::isLiteralExcluded(std::string name, std::vector<std::string>& names)
{
	for (std::string::size_type i = 0; i < names.size(); i++)
	{
		if (isEqual(names[i], name))
			return false;
	}
	return true;
}